$(document).ready(function() {
	var speed = 150;
	
	function bindRotation() {
		$('div#control-buttons .deal').click(function(e) {
			$('ul.playing-cards li').each(function(){
				rotateCard(speed, test());
				});
			});
	}
	
	
	bindRotation();

});

function rotateCard(sp, conts) {
	//var i = 1;
	for (var i = 0; i < 5; i++){
	$('#c'+i).flip({
		direction: 'lr',
		bgColor: '#FFFFFF',
		speed: sp,
		content: (conts[i])			
	});
	setTimeout($('#c'+i), sp);
	}
}


function showInfo(){
	
}

////select box
$(function(){
			
	$('select#speedA').selectmenu({
		style:'dropdown', 
		menuWidth: 140,
		format: addressFormatting
	});
	
	$('select#speedAa').selectmenu({maxHeight: 150});
	
	$('select#speedB').selectmenu({
		width: 300,
		format: addressFormatting
	});
	
	$('select#speedC').selectmenu({style:'dropdown'});
	
	$('select#speedD').selectmenu({
		style:'dropdown', 
		menuWidth: 400,
		format: addressFormatting
	});
	
	$('select#files, select#filesC').selectmenu({
		icons: [
			{find: '.script', icon: 'ui-icon-script'},
			{find: '.image', icon: 'ui-icon-image'}
		]
	});
	
	$('select#filesB').selectmenu({
		icons: [
			{find: '.video'},
			{find: '.podcast'},
			{find: '.rss'}
		]
	});
	
	
});
		
		
//a custom format option callback
var addressFormatting = function(text){
	var newText = text;
	//array of find replaces
	var findreps = [
		{find:/^([^\-]+) \- /g, rep: '<span class="ui-selectmenu-item-header">$1</span>'},
		{find:/([^\|><]+) \| /g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
		{find:/([^\|><\(\)]+) (\()/g, rep: '<span class="ui-selectmenu-item-content">$1</span>$2'},
		{find:/([^\|><\(\)]+)$/g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
		{find:/(\([^\|><]+\))$/g, rep: '<span class="ui-selectmenu-item-footer">$1</span>'}
	];
	
	for(var i in findreps){
		newText = newText.replace(findreps[i].find, findreps[i].rep);
	}
	return newText;
}

function test()
{
	//alert(responseText);
   	var splittedResult = null; // ovde parsirash string - na prvom mestu je dobitak koji poredish sa selekcijom u combo-boxu, a drugi deo su karte
   	var cont1 = '<div class="card rank-5 spades"><span class="rank">5</span><span class="suit">&spades;</span></div>';
	var cont2 = '<div class="card rank-6 spades"><span class="rank">6</span><span class="suit">&spades;</span></div>';
	var cont3 = '<div class="card rank-7 spades"><span class="rank">7</span><span class="suit">&spades;</span></div>';
	var cont4 = '<div class="card rank-8 spades"><span class="rank">8</span><span class="suit">&spades;</span></div>';
	var cont5 = '<div class="card rank-9 spades"><span class="rank">9</span><span class="suit">&spades;</span></div>';

	
	var cont = new Array(cont1,cont2,cont3, cont4, cont5);
    var ajaxRequest = new ajaxObject('http://localhost:1234/test', testReceivedHandler);
    ajaxRequest.update();
    
    return cont;
}
function testReceivedHandler(responseText, responseStatus)
{  
    
	
	//setCards(splittedResult[1]);
	//notifyClient(splittedResult[0]);
}
function notifyClient(score)
{
   //poredish score sa trenutnom selekcijom u combo-boxu, i izbacujesh alert
   //mozesh, ako hocesh, da pravish sistem sa ulozima, pa da smanjujesh i povecavash pare u zavisnosti od dobitka
   //ako je pogodak, onda pochni kockanje manja-veca
  //startNumberCatch();
}
function startNumberCatch()
{
    //div za kockanje se prikazuje
    //pritiska se dugme manji ili veci
    //generishe se sluchajni broj od 1-14
    //ako je broj manji od 7, a pritisnuto je dugme manji, onda se to gleda kao pogodak, i proces pogadjanja se obnavlja
    //ako je broj veci od 7, a pritisnuto je dugme za manji, onda je to gubitak, i kockanje se prekida. (isto vazi i u suprotnom sluchaju za pritisnuto dugme veci).
}
function setCards(cards)
{
   for (i = 1; i < 6; i++)
  {
       var id = "card" + i.toString();
       var cardUrl = null; //parsiraj dobijeni string "cards" ovde i pravi url u fazonu imeKarte_znak
       var elem = document.getElementById(id);
       elem.src = cardUrl;
  }
}	
 
			